﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TangoFamilyAndGuide.Site
{
    public partial class HousingModificar : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Entidades.Sesion sesion;
            if (!IsPostBack)
            {
                if (Funciones.SessionTimeOut(Session))
                {
                    Response.Redirect("~/SessionTimeout.aspx");
                }
                else
                {
                    sesion = (Entidades.Sesion)Session["Sesion"];
                    Entidades.Housing housing = (Entidades.Housing)Session["Housing"];
                    
                    TypePlaceDropDownList.DataSource = Entidades.TypesPlace.TypePlace.ListaMasSinInformar();
                    TypeRoomDropDownList.DataSource = Entidades.TypesRoom.TypeRoom.ListaMasSinInformar();
                    TypeBathroomDropDownList.DataSource = Entidades.TypesBathroom.TypeBathroom.ListaMasSinInformar();
                    HousingOfferDropDownList.DataSource = Entidades.TypesSex.TypeSex.ListaMasSinInformar();
                    MonedaDropDownList.DataSource = Entidades.CodigosMoneda.CodigoMoneda.ListaReducidaMasSinInformar();
                    ServicesListBox.DataSource = Entidades.Services.Service.Lista();

                    IdHousingLabel.Text = housing.Id.ToString();
                    EstadoActualLabel.Text = "Current status: " + housing.Estado;

                    sesion.Usuario.Id = housing.Usuario.Id;
                    DescripcionTextBox.Text = housing.Descripcion;
                    Domicilio.Direccion = housing.Direccion;
                    Domicilio.Pais = housing.Pais;
                    Domicilio.Provincia = housing.Provincia;
                    Domicilio.Localidad = housing.Localidad;
                    Domicilio.CodPost = housing.CodPost;

                    Contacto.Nombre = housing.Contacto.Nombre;
                    Contacto.Email = housing.Contacto.Email;
                    Contacto.Telefono = housing.Contacto.Telefono;

                    ComentariosTextBox.Text = housing.Comentarios;
                    GoogleMapTextBox.Text = housing.GoogleMap;

                    NeighborhoodTextBox.Text = housing.Neighborhood;
                    //ApproximateAddress.Text = housing.ApproximateAddress;
                    SizeSgmTextBox.Text = housing.SizeSgm;
                    TypePlaceDropDownList.SelectedValue = housing.TypePlace;
                    TypeRoomDropDownList.SelectedValue = housing.TypeRoom;
                    TypeBathroomDropDownList.SelectedValue = housing.TypeBathroom;
                    HousingOfferDropDownList.SelectedValue = housing.HousingOffer;
                    SkypeAddressMapTextBox.Text = housing.SkypeAddress;
                    MonedaDropDownList.SelectedValue = housing.Moneda;
                    PricePerDayTextBox.Text = housing.PricePerDay.ToString();
                    PricePerMonthTextBox.Text = housing.PricePerMonth.ToString();
                    //PriceComment.Text = housing.PriceComment;

                    DescripcionTextBox.Focus();
                    DataBind();
                    GenerarServicios(housing.Servicios);
                }
            }
        }
        private void GenerarServicios(string Styles)
        {
            string[] lista = Styles.Split(Convert.ToChar("-"));
            foreach (string texto in lista)
            {
                foreach (ListItem item in ServicesListBox.Items)
                {
                    if (texto == item.Value)
                    {
                        item.Selected = true;
                    }
                }
            }
        }
        protected void AceptarButton_Click(object sender, EventArgs e)
        {
            if (Funciones.SessionTimeOut(Session))
            {
                Response.Redirect("~/SessionTimeout.aspx");
            }
            else
            {
                Entidades.Sesion sesion = (Entidades.Sesion)Session["Sesion"];
                Entidades.Housing housingDesde = (Entidades.Housing)Session["Housing"];
                Entidades.Housing housingHasta = RN.Housing.ObtenerCopia(housingDesde);
                try
                {
                    housingHasta.Id = Convert.ToInt32(IdHousingLabel.Text);
                    housingHasta.Usuario.Id = sesion.Usuario.Id;
                    housingHasta.Descripcion = DescripcionTextBox.Text;
                    housingHasta.Direccion = Domicilio.Direccion;
                    housingHasta.Pais = Domicilio.Pais;
                    housingHasta.Provincia = Domicilio.Provincia;
                    housingHasta.Localidad = Domicilio.Localidad;
                    housingHasta.CodPost = Domicilio.CodPost;

                    housingHasta.Contacto.Nombre = Contacto.Nombre;
                    housingHasta.Contacto.Email = Contacto.Email;
                    housingHasta.Contacto.Telefono = Contacto.Telefono;

                    housingHasta.Comentarios = ComentariosTextBox.Text;
                    housingHasta.GoogleMap = GoogleMapTextBox.Text;

                    housingHasta.Neighborhood = NeighborhoodTextBox.Text;
                    //Housing.ApproximateAddress ApproximateAddress.Text;
                    housingHasta.SizeSgm = SizeSgmTextBox.Text;
                    housingHasta.TypePlace = TypePlaceDropDownList.SelectedValue;
                    housingHasta.TypeRoom = TypeRoomDropDownList.SelectedValue;
                    housingHasta.TypeBathroom = TypeBathroomDropDownList.SelectedValue;
                    housingHasta.HousingOffer = HousingOfferDropDownList.SelectedValue;
                    housingHasta.SkypeAddress = SkypeAddressMapTextBox.Text;
                    housingHasta.Moneda = MonedaDropDownList.SelectedValue;
                    housingHasta.PricePerDay = Convert.ToDecimal(PricePerDayTextBox.Text);
                    housingHasta.PricePerMonth = Convert.ToDecimal(PricePerMonthTextBox.Text);
                    //Housing.PriceComment = PriceComment.Text;
                    string services = "";
                    for (int i = 0; i < ServicesListBox.Items.Count; i++)
                    {
                        if (ServicesListBox.Items[i].Selected)
                        {
                            services += ServicesListBox.Items[i].Value + "-";
                        }
                    }
                    housingHasta.Servicios = services;

                    RN.Housing.Modificar(housingDesde, housingHasta, sesion);

                    DescripcionTextBox.Enabled = false;
                    Domicilio.Enabled = false;
                    Contacto.Enabled = false;
                    ComentariosTextBox.Enabled = false;
                    GoogleMapTextBox.Enabled = false;
                    AceptarButton.Enabled = false;
                    SalirButton.Text = "Salir";

                    MensajeLabel.Text = "There was successfully. ";
                }
                catch (Exception ex)
                {
                    MensajeLabel.Text = EX.Funciones.Detalle(ex);
                    return;
                }
            }
        }
    }
}